package com.ucap.weking.guangan.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ucap.weking.guangan.R;
import com.ucap.weking.guangan.modle.ItemModel;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import okhttp3.Request;

/**
 * Created by weking on 2017/10/18.
 */

public class LIstAdapter extends BaseAdapter {
    ArrayList<ItemModel> mData;

    public LIstAdapter(ArrayList<ItemModel> mData) {
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public ItemModel getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, null);
            holder = new Holder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        ItemModel item = getItem(position);
        String imageUri = item.getImageId();
        if (item.isImage() && !imageUri.equals("")) {
            Glide.with(parent.getContext())
                    .load(imageUri)
                    .into(holder.ivImage);
            holder.ivImage.setVisibility(View.VISIBLE);
        } else {
            holder.ivImage.setVisibility(View.GONE);
        }
        holder.tvTitle.setText(item.getTitle());
        holder.tvTimer.setText(item.getTimer());
        if (item.isTimer()) {//有些不需要显示时间
            holder.llTimer.setVisibility(View.VISIBLE);
        } else {
            holder.llTimer.setVisibility(View.GONE);
        }

        return convertView;
    }

    class Holder {
        @InjectView(R.id.tv_title)
        TextView tvTitle;
        @InjectView(R.id.tv_timer)
        TextView tvTimer;
        @InjectView(R.id.iv_item_image)
        ImageView ivImage;
        @InjectView(R.id.ll_time)
        LinearLayout llTimer;

        public Holder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
